package com.lwb.ztx.news.widget;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.bean.NewsTopicBean;
import com.lwb.ztx.utils.ImageLoaderUtils;
import com.lwb.ztx.utils.ListUtils;
import com.lwb.ztx.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/5/29.
 */
public class NewsTopicAdapter extends RecyclerView.Adapter {
    private List<NewsTopicBean> beans;
    private Context context;
    private OnTopicSliderClickListener listener;

    public NewsTopicAdapter(Context context, OnTopicSliderClickListener listener) {
        this.context = context;
        this.listener = listener;
    }

    public List<NewsTopicBean> getBeans() {
        return beans;
    }

    public void setBeans(List<NewsTopicBean> beans) {
        this.beans = beans;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_BANNER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.topic_item_banner, parent, false);
            return new BannerViewHolder(view);
        } else if (viewType == TYPE_TEXT) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.topic_item_desc, parent, false);
            return new TextViewHolder(view);
        } else if (viewType == TYPE_SLIDER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.topic_item_slider, parent, false);
            return new SliderViewHolder(view);
        } else if (viewType == TYPE_NEWS_POD) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.topic_item_pod_name, parent, false);
            return new PodViewHolder(view);
        } else if (viewType == TYPE_NEWS) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.news_item_normal, parent, false);
            return new NewsViewHolder(view);
        } else {
            View view = new View(context);
            return new DefalutViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        LogUtils.d("position: " + position + "   " + beans.get(position).getBannerUrl());
        if (holder instanceof BannerViewHolder) {
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            ImageLoaderUtils.display(context, bannerViewHolder.bannerView, beans.get(position).getBannerUrl());
        } else if (holder instanceof TextViewHolder) {
            TextViewHolder textViewHolder = (TextViewHolder) holder;
            textViewHolder.textView.setText(beans.get(position).getIntro());
        } else if (holder instanceof SliderViewHolder) {
            SliderViewHolder sliderViewHolder = (SliderViewHolder) holder;
            final List<NewsBean> sliders = beans.get(position).getSliders();
            List<Fragment> images = new ArrayList<>();
            for(int i=0;i<sliders.size();i++)  {
                NewsImageFragment fragment = new NewsImageFragment(sliders.get(i));
                images.add(fragment);
            }
            sliderViewHolder.sliders = sliders;
            if(sliders.size() > 0) {
                sliderViewHolder.title.setText(sliders.get(0).getTitle());
                sliderViewHolder.index.setText("1/" + sliders.size());
            }
            sliderViewHolder.slider.setAdapter(new NewsSliderAdapter(((NewsTopicActivity)context).getSupportFragmentManager(),images));
        } else if (holder instanceof PodViewHolder) {
            PodViewHolder podViewHolder = (PodViewHolder) holder;
            podViewHolder.textView.setText(beans.get(position).getTitle());
        } else if (holder instanceof NewsViewHolder) {
            NewsViewHolder newsViewHolder = (NewsViewHolder) holder; 
            NewsBean news = beans.get(position).getNewsBean();
            if(news.getThumbnail().equals("")) {
                newsViewHolder.mNewsImage.setVisibility(View.GONE);
            } else {
                newsViewHolder.mNewsImage.setVisibility(View.VISIBLE);
                ImageLoaderUtils.display(context, newsViewHolder.mNewsImage, news
                        .getThumbnail());
            }
            newsViewHolder.mNewsTitle.setText(news.getTitle());
            newsViewHolder.mNewsSource.setText(news.getUpdateTime());
            newsViewHolder.mNewsComments.setText(news.getCommentsAll() + "条评论");
            newsViewHolder.content = news;
            if(news.getType().equals("topic2")) {
                newsViewHolder.mNewsTopic.setVisibility(View.VISIBLE);
            } else {
                newsViewHolder.mNewsTopic.setVisibility(View.GONE);
            }
        }
    }

    public class NewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public View mView;
        @InjectView(R.id.news_item_image)
        public ImageView mNewsImage;
        @InjectView(R.id.news_item_title)
        public TextView mNewsTitle;
        @InjectView(R.id.news_item_source)
        public TextView mNewsSource;
        @InjectView(R.id.news_item_comments)
        public TextView mNewsComments;
        @InjectView(R.id.news_item_topic)
        public ImageView mNewsTopic;

        public NewsBean content;

        public NewsViewHolder(View view) {
            super(view);
            mView = view;
            mView.setOnClickListener(this);
            ButterKnife.inject(this, view);
        }

        @Override
        public void onClick(View v) {
            if(null != listener) {
                listener.OnTopicSliderClick(content);
            }
        }
    }
    
    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView bannerView;

        public BannerViewHolder(View itemView) {
            super(itemView);
            bannerView = (ImageView) itemView.findViewById(R.id.topic_item_banner);
        }
    }

    class TextViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public TextViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView;
        }
    }
    class PodViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public PodViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.topic_item_pod_name);
        }
    }

    class SliderViewHolder extends RecyclerView.ViewHolder implements ViewPager.OnPageChangeListener{
        ViewPager slider;
        TextView title;
        TextView index;
        List<NewsBean> sliders;
        int id = 0;
        public SliderViewHolder(View itemView) {
            super(itemView);
            slider = (ViewPager) itemView.findViewById(R.id.topic_item_slider);
            slider.setOnPageChangeListener(this);
            title = (TextView) itemView.findViewById(R.id.topic_item_title);
            index = (TextView) itemView.findViewById(R.id.topic_item_index);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            title.setText(sliders.get(position).getTitle());
            index.setText((position + 1) + "/" + sliders.size());
            id = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

    interface OnTopicSliderClickListener {
        void OnTopicSliderClick(NewsBean bean);
    }

    @Override
    public int getItemCount() {
        LogUtils.i("size:" + ListUtils.getSize(beans));
        return ListUtils.getSize(beans);
    }

    private final static int TYPE_BANNER = 1;
    private final static int TYPE_TEXT = 2;
    private final static int TYPE_SLIDER = 3;
    private final static int TYPE_NEWS_POD= 4;
    private final static int TYPE_NEWS = 5;

    @Override
    public int getItemViewType(int position) {
        String view = beans.get(position).getView();
        if (view.equals("multiTitle")) {
            return TYPE_BANNER;
        } else if (view.equals("text")) {
            return TYPE_TEXT;
        } else if (view.equals("slider")) {
            return TYPE_SLIDER;
        }else if (view.equals("podName")) {
            return TYPE_NEWS_POD;
        }else if (view.equals("newsBean")) {
            return TYPE_NEWS;
        }
        else {
            return -1;
        }
    }

    class  DefalutViewHolder extends RecyclerView.ViewHolder{

        public DefalutViewHolder(View itemView) {
            super(itemView);
        }
    }

}
